<script setup>
import MyNav from "@/components/MyNav.vue";
import MyHead from "@/components/MyHead.vue";
import MyTable from "@/components/MyTable.vue";
import {onMounted, ref, shallowReactive, shallowRef} from "vue";
import {dateFormat, myDelete, myPage, myRemove, myRemoveBatch} from "@/util";
import { pageApi, pageLikeTitleApi, removeApi, removeBatchApi} from "@/api/SchoolApi";
import MyPager from "@/components/MyPager.vue";
import MyList from "@/components/MyList.vue";
import router from "@/router";
import {excelApi} from "@/api/axios";

// 表格数据
let records = ref();
// 分页数据
let pageInfo = shallowReactive({'pageNum': 1, 'pageSize': 5, 'fn': page});
let schoolTitle=shallowRef();

/* ==================== 分页查询 ==================== */
async function page(pageNum = pageInfo['pageNum'],
                    pageSize = pageInfo['pageSize']) {

  let config={
    'api':pageApi,
    'params':{'pageNum':pageNum,'pageSize':pageSize},
    'records':records,
    'pageInfo':pageInfo
  }
  if(schoolTitle.value){
    config['params']['title']=schoolTitle.value;
    config['api']=pageLikeTitleApi;
  }
  await myPage(config);
}

/* ==================== 单条添加 ==================== */
function insert(record) {
  router.push("/rms/SchoolInsert")
}

/* ==================== 单条修改 ==================== */
function update(record) {
  sessionStorage.setItem('school',JSON.stringify(record));
  router.push("/rms/SchoolUpdate")
}

/* ==================== 单条删除 ==================== */
async function remove(record) {
  await myRemove({
    'id': record['id'],
    'api': removeApi,
    'fn': page
  });
}

/* ==================== 打印报表 ==================== */
function excel(record) {
  excelApi();
}

/* ==================== 批量删除 ==================== */

let ids = shallowRef();

async function removeBatch() {
  await myRemoveBatch({
    'ids': ids.value,
    'api': removeBatchApi,
    'fn': page
  });
}

/* ==================== 查看详情 ==================== */
const detailDrawer = shallowRef();
let detailItems = ref();

function detail(record) {
  detailItems.value = [
    {'label': '学校名称', 'value': record['title']},
    {'label': '学院名称', 'value': record['college']},
    {'label': '专业名称', 'value': record['major']},
    {'label': '班级名称', 'value': record['clazz']},
    {'label': '学校地址', 'value': record['address'], 'type': 'card'},
    {'label': '学校描述', 'value': record['info'], 'type': 'card'},
    {'label': '创建时间', 'value': record['created'], 'format': dateFormat},
    {'label': '修改时间', 'value': record['updated'], 'format': dateFormat},
     ];
  // 打开抽屉
  detailDrawer.value = true;
}

/* ==================== 搜索学校名 ==================== */
function pageByTitle(record) {
  schoolTitle.value=record;
  page();

}

/* ==================== 加载函数 ==================== */
onMounted(() => {
  page();
});

</script>

<template v-if="records">

  <my-nav :items="[
      {'icon': 'User', 'label': '员工管理'},
      {'icon': 'User', 'label': '学校列表'},
  ]"/>

  <my-head :items="[
    {'type': 'ipt', 'span': 5, 'placeholder': '学校名称', 'fn': pageByTitle},
    {'type': 'insertBtn', 'offset': 16, 'fn': insert},
    {'type': 'deleteBtn', 'fn': removeBatch},
    {'type': 'excelBtn', 'fn': ()=>excelApi('/school/excel','学校统计表.xlsx')},
  ]"/>

  <my-table :records="records" :columns="[
    {'column': '学校名称', 'property': 'title'},
    {'column': '学院名称', 'property': 'college'},
    {'column': '专业名称', 'property': 'major'},
    {'column': '班级名称', 'property': 'clazz'},
    {'column': '学校名称', 'property': 'title'},
    {'column': '学校地址', 'property': 'address', 'type': 'card'},
    {'column': '学校描述', 'property': 'info', 'type': 'card'},
  ]" :buttons="[
    {'type': 'info', 'fn': detail},
    {'type': 'update', 'fn': update},
    {'type': 'delete', 'fn': remove},
  ]" :selection-change="rows => ids = rows.map(e => e['id'])"/>

  <my-pager :page-info="pageInfo"/>

  <el-drawer v-model="detailDrawer" size="30%">
    <my-list title="记录详情" :items="detailItems"></my-list>
  </el-drawer>

</template>

<style scoped lang="scss">

</style>